<template>
  <div>
    <table class="table table-bordered">
      <tbody>
        <tr>
          <td>医生姓名</td>
          <td><input type="text" v-model="info.yname" /></td>
        </tr>
        <tr>
          <td>头像</td>
          <td>
            <input type="file" @change="Img" />
            <img
              :src="info.yimg"
              width="60"
              height="60"
              v-if="info.yimg != ''"
            />
          </td>
        </tr>
        <tr>
          <td>科室</td>
          <td>
            <select v-model="info.kid">
              <option value="">请选择</option>
              <option v-for="a in date.list" :value="a.kid">
                {{ a.kname }}
              </option>
            </select>
          </td>
        </tr>
        <tr>
          <td></td>
          <td><input type="button" value="添加" @click="Add" /></td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup lang="ts">
import axios from "axios";
import { reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import moment from "moment";
const route = useRoute();
const router = useRouter();
let info: any = reactive({
  yname: "",
  yimg: "",
  kid: "",
});
let date: any = reactive({
  list: [],
});
onMounted(() => {
  Find();
});
const Find = () => {
  axios({
    url: "/api/Yesheng/Find",
    method: "get",
  }).then((res) => {
    console.log(res);
    date.list = res.data;
  });
};
const Img = (e: any) => {
  info.yimg = "";
  let f = e.target.files[0];
  let fs = new FormData();
  fs.append("file", f);
  axios({
    url: "/api/Yesheng/Img",
    method: "post",
    data: fs,
  }).then((res) => {
    console.log(res);
    if (res.data == "不是图片") {
      alert("不是图片");
      return;
    }
    if (res.data == "大于2M") {
      alert("大于2M");
      return;
    }

    info.yimg = res.data;
  });
};
const Add = () => {
  if (info.yname == "") {
    alert("医生姓名不能为空");
    return;
  }
  axios({
    url: "/api/Yesheng/Add",
    method: "post",
    data: info,
  }).then((res) => {
    if (res.data > 0) {
      alert("添加成功");
      router.push("/listyesheng");
      return;
    } else {
      alert("添加失败");
      return;
    }
  });
};
</script>

<style scoped></style>
